package com.bytebuddytest.custominstrumentation;

import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;

/**
 * @author yutianhong
 * @version 1.0
 * @since 2023/12/28 16:17
 */
public enum IntegerSum implements StackManipulation {

    INSTANCE; // singleton

    @Override
    public boolean isValid() {
        return true;
    }

    @Override
    public Size apply(MethodVisitor methodVisitor,
                      Implementation.Context implementationContext) {
        methodVisitor.visitInsn(Opcodes.IADD);
        // -1 代表操作对栈内元素的影响：弹出栈顶两个元素相加并把结果存入栈顶，栈深度-1
        // 0 代表操作对栈最大深度的影响：IADD不会影响最大栈深度，所以为0
        return new Size(-1, 0);
    }
}
